package de.nroz.view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.math.BigDecimal;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;

import de.nroz.model.ArtikelModel;
import de.nroz.model.Images;

public class ViewGewerbe
{
	/**
     * 
     */
	private static final long serialVersionUID = 2878976119135891311L;

	/**
	 * Buttons
	 */

	private JFrame view = null;

	private JTable table = null;

	private JScrollPane scrollPane = null;

	private final JPanel pnlInformation = new JPanel();

	private final JLabel lblAnzEinkauf = new JLabel("Anz. Einkauf:");

	private final JLabel numAnzEinkauf = new JLabel("0");

	private final JLabel lblAnzVerkauf = new JLabel("Anz. Verkauf:");

	private final JLabel numAnzVerkauf = new JLabel("0");

	private final JLabel lblBetrEinkauf = new JLabel("Betr. Einkauf:");

	private final JLabel decBetrEinkauf = new JLabel("0 \u20AC");

	private final JLabel lblBetrVerkauf = new JLabel("Betr. Verkauf:");

	private final JLabel decBetrVerkauf = new JLabel("0 \u20AC");
	
	private final JMenuBar menubar = new JMenuBar();
	
	private final JButton btnInsertArtikel = new JButton();

	private final JButton btnDeleteArtikel = new JButton();
	
	private final JButton btnEditArtikel = new JButton();
	
	private final JButton btnCloneArtikel = new JButton();
	
	private final JMenuItem menuInsertArtikel = new JMenuItem();

	private final JMenuItem menuDeleteArtikel = new JMenuItem();
	
	private final JMenuItem menuEditArtikel = new JMenuItem();
	
	private final JMenuItem menuCloneArtikel = new JMenuItem();
	
	private final JPopupMenu popupmenu = new JPopupMenu();
	
	public ViewGewerbe(ArtikelModel model)
	{
		table = new JTable(model);

		scrollPane = new JScrollPane(table);
		view = new JFrame();
		initTable();

		view.getContentPane().add(pnlInformation, BorderLayout.SOUTH);
		pnlInformation.setLayout(new GridLayout(1, 0, 0, 0));
		pnlInformation.add(lblAnzEinkauf);
		numAnzEinkauf.setHorizontalAlignment(SwingConstants.LEFT);
		pnlInformation.add(numAnzEinkauf);
		pnlInformation.add(lblAnzVerkauf);
		numAnzVerkauf.setHorizontalAlignment(SwingConstants.LEFT);
		pnlInformation.add(numAnzVerkauf);
		pnlInformation.add(lblBetrEinkauf);
		decBetrEinkauf.setHorizontalAlignment(SwingConstants.LEFT);
		pnlInformation.add(decBetrEinkauf);
		pnlInformation.add(lblBetrVerkauf);
		decBetrVerkauf.setHorizontalAlignment(SwingConstants.LEFT);
		pnlInformation.add(decBetrVerkauf);
		view.getContentPane().add(scrollPane, BorderLayout.CENTER);

//		btnInsertArtikel.setBorderPainted(false);
//		btnDeleteArtikel.setBorderPainted(false);
//		btnEditArtikel.setBorderPainted(false);
		menubar.add(btnEditArtikel);
		menubar.add(btnInsertArtikel);
		menubar.add(btnDeleteArtikel);
		menubar.add(btnCloneArtikel);
		popupmenu.add(menuEditArtikel);
		popupmenu.add(menuInsertArtikel);
		popupmenu.add(menuDeleteArtikel);
		popupmenu.add(menuCloneArtikel);
		view.getContentPane().add(menubar,BorderLayout.NORTH);
		
		view.setSize(900, 600);
		view.setTitle("JGewerbe");
		view.setVisible(true);
		view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void showPopup(Component c, int x, int y)
	{
		popupmenu.show(c, x, y);
	}
	
	private void initTable()
	{
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		table.setDefaultRenderer(Boolean.class, new BooleanCellRenderer());
		table.setSelectionBackground(Images.LIGHT_GRAY);
		table.setAutoCreateRowSorter(true);
		initColumnSize();
	}
	
	public void addEditAction(Action a)
	{
		btnEditArtikel.setAction(a);
		menuEditArtikel.setAction(a);
	}
	
	public void addDeleteAction(Action a)
	{
		btnDeleteArtikel.setAction(a);
		menuInsertArtikel.setAction(a);
	}
	
	public void addInsertAction(Action a)
	{
		btnInsertArtikel.setAction(a);
		menuDeleteArtikel.setAction(a);
	}
	
	public void addCloneAction(Action a)
	{
		btnCloneArtikel.setAction(a);
		menuCloneArtikel.setAction(a);
	}

	private void initColumnSize()
	{

		// setColumnSize(0, 100, 200, true);
		// setColumnSize(1, 65, 65, false);
		// setColumnSize(2, 35, 35, false);
		// setColumnSize(3, 65, 65, false);
		// setColumnSize(4, 75, 150, true);
		// setColumnSize(5, 65, 65, false);
		// setColumnSize(6, 75, 150, true);
		// setColumnSize(7, 75, 150, true);
		// setColumnSize(8, 40, 40, false);

		setColumnSize(0, 100, 200, true);
		setColumnSize(1, 65, 65, false);
		setColumnSize(2, 35, 35, false);
		setColumnSize(3, 65, 65, false);
		setColumnSize(4, 75, 150, true);
		setColumnSize(5, 65, 65, false);
		setColumnSize(6, 75, 150, true);
		setColumnSize(7, 75, 150, true);
		setColumnSize(8, 40, 40, false);

	}

	private void setColumnSize(int column, int min, int max, boolean resizable)
	{
//		TableColumnModel t = table.getColumnModel();
//		t.getColumn(column).setMinWidth(min);
//		t.getColumn(column).setMaxWidth(max);
//		t.getColumn(column).setResizable(resizable);
//		t.getColumn(column).setPreferredWidth(min);
	}

	public JTable getTableView()
	{
		return table;
	}

	public JFrame getFrame()
	{
		return view;
	}

	public Component getComponent()
	{
		return view;
	}
	
	public void setTotals(long totalEk, long totalVk, BigDecimal totalEkPreis, BigDecimal totalVkPreis)
	{
		numAnzEinkauf.setText(String.valueOf(totalEk));
		numAnzVerkauf.setText(String.valueOf(totalVk));
		decBetrEinkauf.setText(totalEkPreis.toString());
		decBetrVerkauf.setText(totalVkPreis.toString());
	}

}
